function FirstAssistant() {
}

// we could check if the bosh url is valid ..
FirstAssistant.prototype.onConnect = function() {
	say('onConnect');
	var c = cookie.get(), btn = this.controller.get('connect');

	xmpp.connect(c.url, c.jid, c.pw);
	xmpp.bind('CONNECTED', function() {
		say('assistant connected');
		epic.flash('info', 'connected!');
		btn.mojo.deactivate();
	});
	xmpp.bind('transition', function() {
		epic.flash('info', xmpp.getState().name);
	});
};

FirstAssistant.prototype.onDisconnect = function() {
	say('onDisconnect');
	var btn = this.controller.get('disconnect');
	xmpp.bind('DISCONNECTED', function() {
		epic.flash('info', 'disconnected!');
		btn.mojo.deactivate();
	});
	xmpp.disconnect();
};

FirstAssistant.prototype.onXMPPLog = function() {
	this.controller.stageController.pushScene('xmpplog');
};

FirstAssistant.prototype.onRegister = function() {
	var c = this.controller, api = epic.api, flash = epic.flash;
	api.register({
		button: c.get('register'),
		success: function(data) {
			flash('info', 'created user: ' + jQuery('id',data).text());
			api.storeUser(data);
		}
	});
};

FirstAssistant.prototype.onUnregister = function() {
	var c = this.controller, api = epic.api, flash = epic.flash;
	api.unregister({
		button: c.get('unregister'),
		success: function(data) {
			flash('info', 'unregistered user: ' + cookie.get().id);
			cookie.destroy();
		}
	});
};

FirstAssistant.prototype.setup = function() {
	var c = this.controller, e = Mojo.Event, btn = { type: Mojo.Widget.activityButton };
	var servers = epic.servers, api = epic.api;

	c.setupWidget('servers', servers.options, servers.model);
	c.setupWidget('register', btn, { buttonLabel: 'register' });
	c.setupWidget('unregister', btn, { buttonLabel: 'unregister' });
	c.setupWidget('connect', btn, { buttonLabel: 'connect' });
	c.setupWidget('disconnect', btn, { buttonLabel: 'disconnect' });
	c.setupWidget('xmpp-log', {}, { buttonLabel: 'Log Messages' });

	c.listen('servers', e.propertyChange, api.updateURL);
	c.listen('register', e.tap, this.onRegister.bind(this));
	c.listen('unregister', e.tap, this.onUnregister.bind(this));
	c.listen('connect', e.tap, this.onConnect.bind(this));
	c.listen('disconnect', e.tap, this.onDisconnect.bind(this));
	c.listen('xmpp-log', e.tap, this.onXMPPLog.bind(this));


	api.updateURL();

	say('setup completed.');
};

FirstAssistant.prototype.activate = function(event) {
	/* put in event handlers here that should only be in effect when this scene is active. For
	   example, key handlers that are observing the document */
};

FirstAssistant.prototype.deactivate = function(event) {
	/* remove any event handlers you added in activate and do any other cleanup that should happen before
	   this scene is popped or another scene is pushed on top */
};

FirstAssistant.prototype.cleanup = function(event) {
	/* this function should do any cleanup needed before the scene is destroyed as 
	   a result of being popped off the scene stack */
};
